package com.haoshuang.zhouzhoubang.ui.page.order.rework.refuse

import android.content.Context
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ImageUtils
import com.haoshuang.zhouzhoubang.databinding.ActivityRefuseReworkBinding
import com.haoshuang.zhouzhoubang.ui.adapter.ChooseImageAdapter
import com.haoshuang.zhouzhoubang.utils.MultiMediaChooseUtils
import com.haoshuang.zhouzhoubang.utils.MultiMediaChooseUtils.checkImage
import com.haoshuang.zhouzhoubang.utils.OSSUploadUtils
import com.luck.picture.lib.entity.LocalMedia
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import java.util.ArrayList

/**
 *  拒绝返工
 */
class RefuseReworkActivity : BaseVMActivity<RefuseReworkViewModel, ActivityRefuseReworkBinding>() {

    companion object {
        fun launch(context: Context, orderId: String) {
            context.startPage<RefuseReworkActivity>(
                "orderId" to orderId
            )
        }
    }

    private val orderId by lazy {
        intent.getStringExtra("orderId") ?: ""
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "返工处理"
        binding.titleBar.leftImageView().click { finish() }

        binding.tvSubmit.clickWithTrigger {
            val content = binding.etContent.text.toString()
            val imageList = binding.recyclerView.getMediaList()
            if (imageList.isEmpty()) {
                toast("请选择照片")
                return@clickWithTrigger
            }
            if (content.isEmpty()) {
                toast("请输入不同意理由")
                return@clickWithTrigger
            }
            val pics = imageList.map { it.url }.joinToString(",")
            vm.masterRejectApplyRework(orderId, binding.etContent.text.toString(), pics)
        }
    }

    override fun initData() {
    }

    override fun startObserve() {
        vm.masterRejectApplyReworkData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                setResult(200)
                finish()
            }
        }
    }

}